package com.jaychen.tliaswebmanagement.controller;

import com.jaychen.tliaswebmanagement.pojo.PageResult;
import com.jaychen.tliaswebmanagement.pojo.Result;
import com.jaychen.tliaswebmanagement.pojo.StuQueryParam;
import com.jaychen.tliaswebmanagement.pojo.Student;
import com.jaychen.tliaswebmanagement.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/students")
public class StuController {
    @Autowired
    private StuService stuService;

    @GetMapping
    public Result page(StuQueryParam stuQueryParam){
        log.info("学员列表查询: {}", stuQueryParam);
        PageResult<Student> pageResult = stuService.page(stuQueryParam);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer>  ids){
        log.info("删除学员: {}",ids);
        stuService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Student stu){
        log.info("新增学员: {}",stu);
        stuService.add(stu);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询学员: {}",id);
        Student student = stuService.getById(id);
        return Result.success(student);
    }

    @PutMapping
    public Result update(@RequestBody Student stu){
        log.info("修改学员信息: {}",stu);
        stuService.update(stu);
        return Result.success();
    }

    /**
     * 违纪处理
     * @param id：学员ID
     * @param score：扣除分数
     */
    @PutMapping("/violation/{id}/{score}")
    public Result violate(@PathVariable Integer id, @PathVariable Integer score){
        log.info("");
        stuService.violate(id,score);
        return Result.success();
    }

}
